package com.zmn.oms.services.impl.discount;

import com.google.common.collect.Lists;
import com.zmn.common.constant.FicoConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.model.entity.discount.OrderDiscount;
import com.zmn.oms.model.entity.discount.OrderDiscountQuery;
import com.zmn.oms.persistence.interfaces.discount.OrderDiscountDao;
import com.zmn.oms.services.interfaces.discount.OrderDiscountService;
import io.shardingsphere.api.HintManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：订单优惠
 *
 * @author lujia
 * @date 2018/11/28 14:24
 */
@Service
public class OrderDiscountServiceImpl implements OrderDiscountService {

	@Resource
	private OrderDiscountDao orderDiscountDao;

	@Override
	public List<OrderDiscount> listOrderDiscountByOrderIdList(List<Long> orderIdList) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderDiscountDao.listOrderDiscountByOrderIdList(orderIdList);
		}
	}

	/**
	 * 根据订单id和类别获取订单优惠
	 *
	 * @param orderId
	 * @param categ
	 * @return
	 */
	@Override
	public List<OrderDiscount> listByOrderIdAndCateg(Long orderId, Integer categ) {
		OrderDiscountQuery query = new OrderDiscountQuery();
		query.setOrderId(orderId);
		query.setCateg(categ);
		return orderDiscountDao.listByQuery(query);
	}

	/**
	 * 根据订单id和类别获取订单优惠
	 *
	 * @param orderId
	 * @param categ
	 * @return
	 */
	@Override
	public List<OrderDiscount> listByOrderIdAndCategSrcMaster(Long orderId, Integer categ) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			OrderDiscountQuery query = new OrderDiscountQuery();
			query.setOrderId(orderId);
			query.setCateg(categ);
			return orderDiscountDao.listByQuery(query);
		}
	}

	/**
	 * 通过工单获取优惠列表
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public List<OrderDiscount> listByOrderId(Long orderId) {
		return orderDiscountDao.listByOrderId(orderId);
	}

	@Override
	public List<OrderDiscount> listByOrderIdSrcMaster(Long orderId) {
		try (HintManager hintManager = HintManager.getInstance()) {
			hintManager.setMasterRouteOnly();
			return orderDiscountDao.listByOrderId(orderId);
		}
	}

	@Override
	public Integer insert(OrderDiscount record) {
		record.setItemId(Optional.ofNullable(record.getItemId()).orElse(0));
		record.setItemCode(StringUtils.defaultString(record.getItemCode()));
		record.setThirdCode(Optional.ofNullable(record.getThirdCode()).orElse(GlobalConsts.NO));
		record.setShareFlag(Optional.ofNullable(record.getShareFlag()).orElse(GlobalConsts.NO));
		return orderDiscountDao.insert(record);
	}

	@Override
	public Integer updateSellAmount(OrderDiscount record) {
		return orderDiscountDao.updateSellAmount(record);
	}

	/**
	 * 删除
	 *
	 * @param record
	 * @return
	 */
	@Override
	public Integer deleteByOrderIdAndCateg(OrderDiscount record) {
		return orderDiscountDao.deleteByOrderIdAndCateg(record);
	}

	@Override
	public Integer deleteByArtificialDiscount(Long orderId) {
		List<Integer> artificial = Lists.newArrayList();
		artificial.add(FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_OPERATOR);
		artificial.add(FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_MASTER);
		return orderDiscountDao.deleteByArtificialDiscount(orderId, artificial);
	}

	@Override
	public Integer deleteByOrderId(Long orderId) {
		Objects.requireNonNull(orderId);
		return orderDiscountDao.deleteByOrderId(orderId);
	}

	@Override
	public Integer deleteByKey(Long discountId) {
		Objects.requireNonNull(discountId);
		return orderDiscountDao.deleteByPrimaryKey(discountId);
	}

	@Override
	public void insertOrderDiscountList(List<OrderDiscount> orderDiscountList) {
		orderDiscountList.forEach(e -> {
			e.setItemId(Optional.ofNullable(e.getItemId()).orElse(GlobalConsts.NONE));
			e.setItemCode(StringUtils.defaultString(e.getItemCode()));
			e.setThirdCode(Optional.ofNullable(e.getThirdCode()).orElse(GlobalConsts.NO));
			e.setShareFlag(Optional.ofNullable(e.getShareFlag()).orElse(GlobalConsts.NO));
			e.setSellAmount(Optional.ofNullable(e.getSellAmount()).orElse(GlobalConsts.NONE));
		});
		orderDiscountDao.insertOrderDiscountList(orderDiscountList);
	}

	@Override
	public void updateByPrimaryKeySelective(OrderDiscount record) {
		orderDiscountDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public void updateBatch(List<OrderDiscount> orderDiscountList) {
		orderDiscountDao.updateBatch(orderDiscountList);
	}
}
